
//
//  ST_DailyMomentModel.swift
//  btvideo
//  使用 http://www.jsoncafe.com/ 生成
//  Created by mac on 2020/7/22.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON


class ST_DailyMomentModel : NSObject{

    var accid : String
    var avatar : String
    var commentCount : Int
    var faceAuditState : Int
    var guardContent : String
    var guardTime : Int
    var id : Int
    var dailyId : Int
    var image : String
    var isThumbUp : Bool
    var name : String
    var lookNum : Int
    var publishNum : Int
    var nickname : String
    var remark : String
    var thumpCount : Int
    var type : Int
    var videoFullDuration : Int
    var videoUrl : String
    var vipExpire : Int
    var createInt : Int
    var contentImages : [DailyMomentMedia]
    var viewCount:Int
    
    
    var distance:String?
    var cityName:String?
    
//    计算后的字段
    var createStr : String
    var isVip:Bool
    var detectedFace:Bool
    
    
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
      
        accid = json["accid"].stringValue
        avatar = json["avatar"].stringValue
        commentCount = json["comment_count"].intValue
        contentImages = json["content_images"].arrayValue.map({ (itemJson) -> DailyMomentMedia in
            return DailyMomentMedia.init(fromJson: itemJson)
        })
        
        faceAuditState = json["face_audit_state"].intValue
        guardContent = json["guard_content"].stringValue
        guardTime = json["guard_time"].intValue
        id = json["id"].intValue
        dailyId = json["daily_id"].intValue
        image = json["image"].stringValue
        isThumbUp = json["is_thumb_up"].boolValue
        name = json["name"].stringValue
        nickname = json["nickname"].stringValue
        remark = json["remark"].stringValue
        thumpCount = json["thump_count"].intValue
        type = json["type"].intValue
        videoFullDuration = json["video_full_duration"].intValue
        videoUrl = json["video_url"].stringValue
        vipExpire = json["vip_expire"].intValue
        createInt = json["create_int"].intValue
        viewCount = json["view_count"].intValue
        
        lookNum = json["look_num"].intValue
        publishNum = json["publish_num"].intValue
        
        distance = json["distance"].string
        cityName = json["city_name"].string
        
        createStr = createInt.double.b_t_global_date_string()
        
        isVip = CHECK_MEMBER_STATUS(expirationTime: vipExpire) == .subscribe
        
        detectedFace = json["face_audit_state"].intValue == 3
        
        super.init()
        
        self.setupLayoutExtension()
    }

    /**
     计算 frame 等
     */
    func setupLayoutExtension() {
        
    }
    
    func conver_to_daily_base_model() -> ST_DailBaseModel {
        
        let dict = ["id":self.dailyId,"name":self.name,"image":self.image,"look_num":self.lookNum,"publish_num":self.publishNum] as [String : Any]
        
        return ST_DailBaseModel.init(fromJson: JSON(dict))
    }
    
}


//MARK: - Hall

/**
 展示推荐样式
 */
class DailyMomentOfHallModel: ST_DailyMomentModel {
    
    /**
     折叠模式下最大高度
     */
    static let contentFoldMaxHeight = 89.cgFloat
    
    /**
     图片或者视频Container 的 Size
     */
    private(set) var mediaSize = CGSize.zero
    
    /**
     标签🏷离顶部的距离  ex  #认真工作
     */
    private(set) var signTopConstraint = 0.cgFloat
    
    /**
     内容离顶部的距离
     */
    private(set) var contentTopConstraint = 0.cgFloat
    
    /**
     默认是折叠模式
     */
    var isFoldContent = true
    
    /**
     内容
     */
    private(set) var contetnAttriStr = NSAttributedString()
    /**
     折叠内容Size
     */
    private(set) var foldContentSize = CGSize.zero
    /**
       展开内容Size
    */
    private(set) var unfoldContentSize = CGSize.zero
    /**
    
     展开Cell 高度
    */
    private(set) var unfoldCellHeight = CGFloat.zero
    /**
    折叠Cell 高度
    */
    private(set) var foldCellHeight = CGFloat.zero
    
    
    var foldClosure:(()->())?
    
    var cellHeight:CGFloat{
        get{
            if isFoldContent{
                return foldCellHeight
            }else{
                return unfoldCellHeight
            }
        }
    }
    
    override func setupLayoutExtension() {
        
        let resMaxWidth = ScreenWidth - 15 - 69
        
        switch contentImages.count {
        case 0:
            mediaSize = CGSize.zero
        case 1:
            if let first = contentImages.first {
                
                let maxW = 200/375*ScreenWidth
//                宽图
                if first.width > first.height{
                    
                    let h = maxW/(first.width < 1 ? 1 : first.width)*first.height
                    
                    mediaSize = CGSize.init(width: maxW, height: h)
                }
//                长图
                else{
                    
                    let w = maxW/(first.height < 1 ? 1 : first.height)*first.width
                    
                    mediaSize = CGSize.init(width: w, height: maxW)
                    
                }
            }
        case 2:
            
            mediaSize = CGSize.init(width: resMaxWidth, height: (resMaxWidth-5)/2.0)
            break
        case 3:
            mediaSize = CGSize.init(width: resMaxWidth, height: (resMaxWidth-10)/3.0)
            break
        default:
            mediaSize = CGSize.init(width: resMaxWidth, height: resMaxWidth)
        }
        
        
        if mediaSize.height > 0{
            signTopConstraint = 60 + 10 + 2 + mediaSize.height + 10
        }else{
            signTopConstraint = 2 + 60 + 10
        }
        
        contentTopConstraint = signTopConstraint + 24 + 10
        
        let originalAtrriStr = NSMutableAttributedString.init(string: self.remark)
        
        originalAtrriStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.init(hex: 0x484848)!,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15),NSAttributedString.Key.paragraphStyle:NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 5)], range: NSRange.init(location: 0, length: originalAtrriStr.length))
        
        let originalHeight = originalAtrriStr.height(withConstrainedWidth: resMaxWidth)
        
//        可以折叠
        if originalHeight > DailyMomentOfHallModel.contentFoldMaxHeight{
//            添加收起
            let foldString = NSMutableAttributedString(string: " 收起")
            
            let foldAttr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 15),.foregroundColor: UIColor(red: 0.26, green: 0.81, blue: 0.85, alpha: 1)]
            
            foldString.addAttributes(foldAttr, range: NSRange(location: 0, length: foldString.length))
            
//            添加收起点击事件
            let hi = YYTextHighlight()
            
            let action:YYTextAction = {[weak self] containerView, text, range, rect in
                
                print("点击收起啦")
                if let fold = self?.isFoldContent{
                    self?.isFoldContent = !fold
                }
                self?.foldClosure?()
                
            }
            
            hi.tapAction = action
            
            foldString.yy_setTextHighlight(hi, range: NSRange(location: 0, length: foldString.length))
            
            originalAtrriStr.append(foldString)
            
            foldContentSize = CGSize.init(width: resMaxWidth, height: DailyMomentOfHallModel.contentFoldMaxHeight)
            
//            展开后的高度
            let unfoldContentHeight = originalAtrriStr.height(withConstrainedWidth: resMaxWidth)
            
            unfoldContentSize = CGSize.init(width: resMaxWidth, height: unfoldContentHeight)
            
        }else{
            
            foldContentSize = CGSize.init(width: resMaxWidth, height: originalHeight)
            
            unfoldContentSize = foldContentSize
        }
         
        contetnAttriStr = originalAtrriStr
        
        foldCellHeight = contentTopConstraint + foldContentSize.height + 57 - 10
        
        unfoldCellHeight = contentTopConstraint + unfoldContentSize.height + 57 - 10
    }
    
}


/**
 个人中心 添加样式
 */
class DailyAppendStyleModel: DailyMomentOfHallModel {
    
    
    override var cellHeight: CGFloat{
        get{
            return 80
        }
    }
}

//MARK: - Other Center

/**
 展示 个人中心样式
 */
class DailyOfSpaceCenterModel: ST_DailyMomentModel {
    
    /**
     content最大高度
     */
    static let contentMaxHeight = 78.cgFloat
    
    /**
     图片或者视频Container 的 Size
     */
    private(set) var mediaSize = CGSize.zero
    
    /**
     标签🏷离顶部的距离  ex  #认真工作
     */
    private(set) var signTopConstraint = 0.cgFloat
    
    /**
     内容离顶部的距离
     */
    private(set) var contentTopConstraint = 0.cgFloat
    
    
    /**
     内容
     */
    private(set) var contetnAttriStr = NSAttributedString()
    /**
     内容Size
     */
    private(set) var contentSize = CGSize.zero
    
    /**
    展开Cell 高度
    */
    private(set) var cellHeight = CGFloat.zero
    
    
    override func setupLayoutExtension() {
        
        let resMaxWidth = ScreenWidth - 15 - 69
        
        switch contentImages.count {
        case 0:
            mediaSize = CGSize.zero
        case 1:
            if let first = contentImages.first {
                
                let maxW = 200/375*ScreenWidth
//                宽图
                if first.width > first.height{
                    
                    let h = maxW/(first.width < 1 ? 1 : first.width)*first.height
                    
                    mediaSize = CGSize.init(width: maxW, height: h)
                }
//                长图
                else{
                    
                    let w = maxW/(first.height < 1 ? 1 : first.height)*first.width
                    
                    mediaSize = CGSize.init(width: w, height: maxW)
                    
                }
            }
        case 2:
            
            mediaSize = CGSize.init(width: resMaxWidth, height: (resMaxWidth-5)/2.0)
            break
        case 3:
            mediaSize = CGSize.init(width: resMaxWidth, height: (resMaxWidth-10)/3.0)
            break
        default:
            mediaSize = CGSize.init(width: resMaxWidth, height: resMaxWidth)
        }
        
        
        if mediaSize.height > 0{
            signTopConstraint = 60 + 10 + 2 + mediaSize.height + 10
        }else{
            signTopConstraint = 2 + 60 + 10
        }
        
        contentTopConstraint = signTopConstraint + 24 + 10
        
        let originalAtrriStr = NSMutableAttributedString.init(string: self.remark)
        
        originalAtrriStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.init(hex: 0x484848)!,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15),NSAttributedString.Key.paragraphStyle:NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 5)], range: NSRange.init(location: 0, length: originalAtrriStr.length))
        
        var originalHeight = originalAtrriStr.height(withConstrainedWidth: resMaxWidth)
        
//        超出最大高度
        if originalHeight > DailyOfSpaceCenterModel.contentMaxHeight{
            
            originalHeight = DailyOfSpaceCenterModel.contentMaxHeight
        }
        
        contentSize = CGSize.init(width: resMaxWidth, height: originalHeight)
         
        contetnAttriStr = originalAtrriStr
        
        cellHeight = contentTopConstraint + contentSize.height + 5
    }
    
}



//MARK: - Media


class DailyMomentMedia: NSObject {
    
    var content:String
    var width:CGFloat
    var height:CGFloat
    var videoFullDuration : Int
    var videoUrl : String
    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        content = json["content"].stringValue
        width = json["width"].floatValue.cgFloat
        height = json["height"].floatValue.cgFloat
        videoFullDuration = json["video_full_duration"].intValue
        videoUrl = json["video_url"].stringValue
        super.init()
    }
    
}
